﻿/// <summary>
/// Author: HBLAi
/// Date: 2013/06/07
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ivs.Core.Mapping;
using SketchTour.Models;
using Ivs.Core.Common.Utilities;

namespace SketchTour.DAL
{
    public class MSST00DAL
    {
        private EFBaseDao _context;
        public MSST00DAL()
        {
            _context = new EFBaseDao();
        }

        public IQueryable<MSST00Models> GetStaffView()
        {
            var query = from v in _context.m_staff
                        select new MSST00Models
                        {
                            Code = v.Code,
                            StaffTypeID = v.m_stafftype.ID,
                            StaffType = v.m_stafftype.StaffTypeName,
                            NameVN = v.NameVN,
                            NameEN = v.NameEN,
                            Address = v.Address,
                            Tel = v.Tel,
                            BirthDay = v.BirthDay,
                            Sex = v.Sex,
                            Email = v.Email,
                            Experience = v.Experience,
                            StaffOfLanguage = v.StaffOfLanguage
                        };
            return query;
        }

        public MSST00Models Search(string code)
        {
            return GetStaffView().Where(v => v.Code == code).Single();
        }

        public IQueryable<m_stafftype> GetStaffType()
        {
            return _context.m_stafftype;
        }

        public int Insert(MSST00Models m_staff)
        {
            int Results;
            m_staff obj = new m_staff();
            obj.Code = m_staff.Code;
            obj.StaffTypeID = m_staff.StaffTypeID;
            obj.NameVN = m_staff.NameVN;
            obj.NameEN = m_staff.NameEN;
            obj.Address = m_staff.Address;
            obj.Tel = m_staff.Tel;
            obj.BirthDay = m_staff.BirthDay;
            obj.Sex = m_staff.Sex;
            obj.Email = m_staff.Email;
            obj.Experience = m_staff.Experience;
            obj.StaffOfLanguage = m_staff.StaffOfLanguage;
            _context.m_staff.AddObject(obj);
            Results = _context.Saves();
            return Results;
        }

        public int Update(MSST00Models m_staff)
        {
            int Results;
            var obj = _context.m_staff.Single(v => v.Code == m_staff.Code);
            obj.Code = m_staff.Code;
            obj.StaffTypeID = m_staff.StaffTypeID;
            obj.NameVN = m_staff.NameVN;
            obj.NameEN = m_staff.NameEN;
            obj.Address = m_staff.Address;
            obj.Tel = m_staff.Tel;
            obj.BirthDay = m_staff.BirthDay;
            obj.Sex = m_staff.Sex;
            obj.Email = m_staff.Email;
            obj.Experience = m_staff.Experience;
            obj.StaffOfLanguage = m_staff.StaffOfLanguage;
            Results = _context.Saves();
            return Results;
        }

        public int Delete(string id)
        {
            m_staff obj = _context.m_staff.Single(v => v.Code == id);
            _context.m_staff.DeleteObject(obj);
            int Results = _context.Saves();
            return Results;
        }

        #region Search
        public int Search(MSST00Models Model, out IEnumerable<MSST00Models> data)
        {
            int returnCode = 0;
            try
            {
                data = from v in _context.m_staff
                       select new MSST00Models
                       {
                           Code = v.Code,
                           StaffTypeID = v.m_stafftype.ID,
                           StaffType = v.m_stafftype.StaffTypeName,
                           NameVN = v.NameVN,
                           NameEN = v.NameEN,
                           Address = v.Address,
                           Tel = v.Tel,
                           BirthDay = v.BirthDay,
                           Sex = v.Sex,
                           Email = v.Email,
                           Experience = v.Experience,
                           StaffOfLanguage = v.StaffOfLanguage

                       };
                if (!Utilities.isNullOrEmpty(Model.Code))
                {
                    data = data.Where(a => a.Code.Contains(Model.Code));
                }
                if (Model.StaffTypeID.HasValue)
                {
                    data = data.Where(a => a.StaffTypeID == Model.StaffTypeID);
                }
                if (!Utilities.isNullOrEmpty(Model.Tel))
                {
                    data = data.Where(a => a.Tel.Contains(Model.Tel));
                }
                if (!Utilities.isNullOrEmpty(Model.NameVN))
                {
                    data = data.Where(a => a.NameVN.Contains(Model.NameVN));
                }
                if (!Utilities.isNullOrEmpty(Model.NameEN))
                {
                    data = data.Where(a => a.NameEN.Contains(Model.NameEN));
                }
                if (!Utilities.isNullOrEmpty(Model.Email))
                {
                    data = data.Where(a => a.Email.Contains(Model.Email));
                }
                if (!Utilities.isNullOrEmpty(Model.Sex))
                {
                    data = data.Where(a => a.Sex == Model.Sex);
                }
                data = data.OrderByDescending(s => s.Code);
                returnCode = data.Count();
            }
            catch (Exception ex)
            {
                returnCode = _context.ProcessDbException(ex);
                data = null;
            }
            return returnCode;

        }
        #endregion Search
    }
}